package com.wgj.android.dialog;

import android.app.Dialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jiaai.xui.view.widget.XToast;
import com.wgj.android.R;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/11/22.
 */

public class HongBaoJCDialog extends DialogFragment {
    private JCCallback jcCallback;

    private View view;
    private Dialog dialog;

    @BindView(R.id.img)
    ImageView jcView;

    @BindView(R.id.goBack)
    Button goBack;

    private int clickCount = 0;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (view == null){
            super.onCreate(savedInstanceState);
            view = LayoutInflater.from(getActivity()).inflate(R.layout.hongbaojc_layout, null, false);
        }

        ButterKnife.bind(this,view);
        jcView.setImageDrawable(getResources().getDrawable(R.mipmap.close_wuyao));
        goBack.setText("下一步");
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        super.onCreateDialog(savedInstanceState);
        if(dialog==null){
            // 使用不带Theme的构造器, 获得的dialog边框距离屏幕仍有几毫米的缝隙。
            initDialogStyle(view);
        }
        return dialog;
    }

    private void initDialogStyle(View view) {
        dialog = new Dialog(getActivity(), R.style.dialog_setting);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(true); // 外部点击取消
        // 设置宽度为屏宽, 靠近屏幕底部。
        Window window = dialog.getWindow();
        window.setWindowAnimations(R.style.popupwindow_anim_style);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.CENTER; // 紧贴底部
        lp.width =WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
    }

    @OnClick(R.id.goBack) void onClick(){
        clickCount++;
        switch (clickCount){
            case 0:
                jcView.setImageDrawable(getResources().getDrawable(R.mipmap.close_wuyao));
                break;
            case 1:
                jcView.setImageDrawable(getResources().getDrawable(R.mipmap.opennews_notice));
                break;
            case 2:
                jcView.setImageDrawable(getResources().getDrawable(R.mipmap.newest_wx));
                break;
        }

        if (clickCount > 2){
            jcCallback.callback();
            clickCount = 0;
            dismiss();
        }

   }

    public HongBaoJCDialog setJCCallback(JCCallback jcCallback) {
        this.jcCallback = jcCallback;
        return  this;
    }

    //教程回调接口
    public  interface  JCCallback{
        void callback();
    }
}
